<template>
  <div class="post-view" @click="lookPostClick">
    <div
      class="post-content"
      :class="{ 'post-content-no-img': !postItem.coverImg }"
    >
      <span class="title">{{ postItem.title }}</span>
      <span class="text">{{ postItem.summary }}</span>
      <div class="post-bottom">
        <div class="left">
          <span>{{ postItem.username }}</span
          >|
          <div class="icon">
            <svg-icon icon-file-name="eye"></svg-icon>
            <span style="margin-left: 5px">{{
              computedPageViewAndLikes(postItem.pageView)
            }}</span>
          </div>
          <div class="icon">
            <svg-icon icon-file-name="like"></svg-icon>
            <span style="margin-left: 5px">{{
              computedPageViewAndLikes(postItem.likeCount)
            }}</span>
          </div>
        </div>
        <div class="right">
          <el-tag
            size="mini"
            type="info"
            effect="plain"
            v-for="(name, index) in postItem.tagNames"
            :key="index"
            >{{ name }}</el-tag
          >
        </div>
      </div>
    </div>
    <div class="post-img" v-if="postItem.coverImg">
      <img :src="postItem.coverImg" alt="" draggable="false" />
    </div>
  </div>
</template>

<script>
import { addPostPageView } from "../../../api/post";
export default {
  props: {
    postItem: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {};
  },
  methods: {
    computedPageViewAndLikes(count) {
      if (count < 1000) {
        return count;
      } else {
        return (count / 1000).toFixed(1) + "k";
      }
    },
    lookPostClick() {
      // 将当前访问的文章浏览量加一
      addPostPageView(this.postItem.postId).then((res) => {
        if (res.data.code === 1) {
          res;
        } else {
          this.$message.error("网络错误");
        }
      });
      // 使用Vue Router获取目标路由的完整URL
      const routePath = this.$router.resolve({
        name: "LookPost",
        params: {
          postId: this.postItem.postId,
          postTitle: this.postItem.title,
        },
      }).href;
      window.open(routePath, "_blank");
    },
  },
  computed: {},
};
</script>

<style lang="less" scoped>
.post-view {
  cursor: pointer;
  width: 100%;
  display: flex;
  padding: 15px 5px 10px 5px;
  box-sizing: border-box;
  border-bottom: 1px solid #f5f5f5;
  &:hover {
    background-color: #f5f5f5;
  }
  .post-content {
    box-sizing: border-box;
    padding-right: 20px;
    padding-left: 10px;
    width: 80%;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    gap: 10px;
    &.post-content-no-img {
      width: 100%;
    }
    .title {
      width: 100%;
      font-size: 17px !important;
      font-weight: bold !important;
    }
    .text {
      font-size: 14px !important;
      color: #8a919f !important;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .post-bottom {
      margin-top: 7px;
      display: flex;
      justify-content: space-between;
      gap: 10px;
      font-size: 14px;
      color: #999;
      .left {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        gap: 10px;
        .icon {
          display: flex;
          justify-content: center;
          align-items: center;
          .svg-icon {
            width: 15px;
            height: 15px;
          }
        }
      }
      .right {
        display: flex;
        justify-content: flex-end;
        align-items: center;
        gap: 10px;
      }
    }
  }
  .post-img {
    img {
      width: 130px;
      height: 88px;
      border-radius: 5px;
    }
  }
}
</style>

<style scoped>
.el-tag--plain.el-tag--info:hover {
  border-color: #1e80ff !important;
  color: #1e80ff !important;
}
</style>